package com.baizhi.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.entity.Comment;
import com.baizhi.service.ICommentService;
import com.baizhi.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentService cs;
    @GetMapping("/selectNumForSchool/{schoolId}")
    public Integer selectNumForSchool(@PathVariable int schoolId){
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.eq("school_id",schoolId);
        return cs.count(wrapper);
    }
    @GetMapping("/selectByPage")
    public IPage<Comment> selectByPage(int pageNo, int pageSize, int schoolId){
        return cs.selectCommentByPage(pageNo,pageSize,schoolId);
    }

    @PostMapping("/add")
    public void add(@RequestBody Comment comment,@RequestHeader("Authorization")String token){
        System.out.println(token);
        DecodedJWT jwt = JwtUtils.verify(token);


        String userIdStr = jwt.getClaim("userid").asString();
        comment.setUserId(Integer.parseInt(userIdStr));
        //获取系统时间给评论时间
        comment.setCommentTime(LocalDateTime.now());
        System.out.println(comment);
        cs.save(comment);
    }

    //点赞
    @PostMapping("/giveLikes")
    public void giveLikes(int commentId){
        cs.giveLikes(commentId);
    }
}

